<%@page import="tu.java.login.User"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" import="java.util.Vector, tu.java.movie.Movie, tu.java.utils.UIUtils, tu.java.movie.Genre, tu.java.movie.MovieImage"%>
    
<%@include file="template/header.jspf" %>


<% 
if(!Menu.checkUserAcess(request, 4)){
	response.sendRedirect(request.getContextPath());
	return;
}
User user = (User) session.getAttribute("userObj");
int userId = user.getUserId();

if (request.getParameter("mode") == null) {
	response.sendRedirect("movies.jsp?mode=search");
}

if (request.getParameter("mode") != null && request.getParameter("mode").equals("save")) {
	Movie movie = Movie.searchMovies("", Integer.parseInt(request.getParameter("id"))).get(0);
	movie.setMovieName(request.getParameter("name"));
	movie.setMovieDescription(request.getParameter("description"));
	movie.setMovieLenght(Integer.parseInt(request.getParameter("lenght")));
	movie.setMovieTypeId(Integer.parseInt(request.getParameter("type")));
	movie.editMovie();
	response.sendRedirect("movies.jsp?mode=edit&id=" + request.getParameter("id"));
}

if (request.getParameter("mode") != null && request.getParameter("mode").equals("addmovie")) {
	Movie newMovie = new Movie();
	newMovie.setMovieName(request.getParameter("name"));
	newMovie.setMovieDescription(request.getParameter("description"));
	newMovie.setMovieLenght(Integer.parseInt(request.getParameter("lenght")));
	newMovie.setMovieTypeId(Integer.parseInt(request.getParameter("type")));
	newMovie.addMovie(userId);
	response.sendRedirect("movies.jsp?mode=edit&id=" + Movie.getLastId());
}

if (request.getParameter("mode") != null && request.getParameter("mode").equals("deleteMovie")) {
	Movie movie = Movie.searchMovies("", Integer.parseInt(request.getParameter("id"))).get(0);
	movie.deleteMovie();
	response.sendRedirect("movies.jsp?mode=search&name=" + request.getParameter("name") + "&submit");
}

if (request.getParameter("mode") != null && request.getParameter("mode").equals("addgenre")) {
	Movie movie = Movie.searchMovies("", Integer.parseInt(request.getParameter("id"))).get(0);
	Genre.addGenre(Integer.parseInt(request.getParameter("genre")), movie.getMovieId());
	response.sendRedirect("movies.jsp?mode=edit&id=" + request.getParameter("id"));
}

if (request.getParameter("mode") != null && request.getParameter("mode").equals("deletegenre")) {
	Movie movie = Movie.searchMovies("", Integer.parseInt(request.getParameter("id"))).get(0);
	Genre.deleteGenre(Integer.parseInt(request.getParameter("genre")), movie.getMovieId());
	response.sendRedirect("movies.jsp?mode=edit&id=" + request.getParameter("id"));
}

if (request.getParameter("mode") != null && request.getParameter("mode").equals("delimg")) {
	MovieImage img = new MovieImage();
	img.setMovieImageId(Integer.parseInt(request.getParameter("img")));
	img.deleteMovieImage();
	response.sendRedirect("movies.jsp?mode=edit&id=" + request.getParameter("id"));
}

%>


<div class="content">


	<% 	
		// ### Search ###
		if (request.getParameter("mode")!=null && request.getParameter("mode").equals("search")) { 
			
			String name = request.getParameter("name") != null ? request.getParameter("name") : "";
			Vector<Movie> movies = null;
				
	%>
		
		<h2 id="title">Търсене на филми</h2>
		
		<form action="movies.jsp?mode=search" method="post" id="search" name="search">
			<table class="formtable">
				<tr>
					<td align="right">Заглавие на филм:</td>
					<td align="left"><input type="text" id="name" name="name" value="<%=name%>"/></td>
				</tr>
				<tr>
					<td></td>
					<td><input type="submit" id="submit" name="submit" value="Търси"/>&nbsp;<input type="button" id="add" name="add" value="Добави нов" onclick="window.location = 'movies.jsp?mode=add';"/></td>
				</tr>
			</table>
		</form>
		
		<% 
			if (request.getParameter("submit")!=null) {
				movies = Movie.searchMovies("%" + name + "%", 0);
			}
		
			if (movies != null) {
		%>
		<table class="resulttable">
			<tr>
				<th width="100">Заглавие</th>
				<th width="200">Описание</th>
				<th width="100">Жанр</th>
				<th width="100">Времетраене</th>
				<th width="100">Тип</th>
				<th width="100">Действие</th>
			</tr>
			<%for (int i=0; i<movies.size(); i++) { %>
			<tr>
				<td><%=movies.get(i).getMovieName()%></td>
				<td><%=movies.get(i).getMovieDescription().length()>50 ? movies.get(i).getMovieDescription().substring(0,49) + "..." : movies.get(i).getMovieDescription()%></td>
				<td><%=movies.get(i).printGenres()%></td>
				<td><%=movies.get(i).getMovieLenght()%></td>
				<td><%=movies.get(i).getMovieType()%></td>
				<td>
					<a href="movies.jsp?mode=edit&id=<%=movies.get(i).getMovieId()%>">Редактирай</a>&nbsp;
					<a href="#" onclick="confirmMsg('Сигурни ли сте, че искате да изтриете този филм?','movies.jsp?mode=deleteMovie&id=<%=movies.get(i).getMovieId()%>&name=<%=name%>&submit');">Изтрий</a>
				</td>
			</tr>
			<%} %>
		</table>
		<%=movies.size()==0 ? "<div class=\"error\">Няма намерени резултати!</div>":""%>
		<%} %>
		
	<%} %>
	
	
	
	<% 	
		// ### Edit ###
		if (request.getParameter("mode")!=null && request.getParameter("mode").equals("edit")) { 
			
			int id = Integer.parseInt(request.getParameter("id"));
			
			Movie movie = Movie.searchMovies("", id).get(0);
	%>
		
		<h2 id="title">Редактиране на филм</h2>
		
		<form action="movies.jsp?mode=save" method="post" id="edit" name="edit" onsubmit="return validateMovieDetails();">
			<input type="hidden" id="id" name="id" value="<%=id%>">
			<table class="formtable">
				<tr>
					<td align="right">Заглавие:</td>
					<td align="left"><input type="text" id="name" name="name" value="<%=movie.getMovieName()%>"/></td>
				</tr>
				<tr>
					<td align="right">Описание:</td>
					<td align="left"><textarea cols="20" rows="5" id="description" name="description"><%=movie.getMovieDescription()%></textarea></td>
				</tr>
				<tr>
					<td align="right">Времетраене (мин.):</td>
					<td align="left"><input type="text" id="lenght" name="lenght" value="<%=movie.getMovieLenght()%>"/></td>
				</tr>
				<tr>
					<td align="right">Тип:</td>
					<td align="left">
						<select id="type" name="type">
							<%=UIUtils.printOptions("select CS01_NOMENCLATURE_ID, CS01_DESCRIPTION from TUDB.TS01_NOMENCLATURES where CS01_NOMENCLATURE_ID_FROM = 200", movie.getMovieTypeId())%>
						</select>
					
					</td>
				</tr>
				<tr>
					<td></td>
					<td><input type="submit" id="submit" name="submit" value="Запази"/>
				</tr>
			</table>
		</form>
		
		<h3 id="title">Жанрове</h3>
		
		<form action="movies.jsp?mode=deletegenre" method="post" id="genresDel" name="genresDel">
			<input type="hidden" id="id" name="id" value="<%=id%>"/>
			<table class="formtable" style="margin-bottom:0px;">
				<tr>
					<td align="right">
						<select id="genre" name="genre">
							<%=UIUtils.printOptions("select CS01_NOMENCLATURE_ID, CS01_DESCRIPTION from TS01_NOMENCLATURES where CS01_NOMENCLATURE_ID in (select CS01_GENRE_ID from TM02_MOVIE_GENRES where CM01_MOVIE_ID = " + id + ") and CS01_NOMENCLATURE_ID_FROM = 300", 0)%>
						</select>
					</td>
					<td align="left">
						<input type="submit" id="submit" name="submit" value="Изтрий"/>
					</td>
				</tr>
			</table>
		</form>
		
		<form action="movies.jsp?mode=addgenre" method="post" id="genresAdd" name="genresAdd">
			<input type="hidden" id="id" name="id" value="<%=id%>"/>
			<table class="formtable">
				<tr>
					<td align="right">
						<select id="genre" name="genre">
							<%=UIUtils.printOptions("select CS01_NOMENCLATURE_ID, CS01_DESCRIPTION from TS01_NOMENCLATURES where CS01_NOMENCLATURE_ID not in (select CS01_GENRE_ID from TM02_MOVIE_GENRES where CM01_MOVIE_ID = " + id + ") and CS01_NOMENCLATURE_ID_FROM = 300", 0)%>
						</select>
					</td>
					<td align="left">
						<input type="submit" id="submit" name="submit" value="Добави"/>
					</td>
				</tr>
			</table>
		</form>
				
		<h3 id="title">Снимки</h3>
		
		<jsp:include page="/includes/fileUploadForm.jsp">
			<jsp:param name="movieId" value="<%=id%>"/>
		</jsp:include>
		<table>
		<%int j=1; int imgPerRow = 4;
		for(int i=0;i<movie.getMovieImages().size();i++){
			MovieImage m = movie.getMovieImages().get(i);
			session.setAttribute("imageContent"+i, m.getMovieImage()); 
			if(j==1){%>
				<tr>
			<%}%>
				<td>
					<img src="/java-tu/includes/showFile.jsp?id=<%=i%>" width="200" height="150">
					<br/><input type="button" id="delImg" name="delImg" value="Изтрий" onclick="window.location = 'movies.jsp?mode=delimg&img=<%=m.getMovieImageId()%>&id=<%=id%>';">
				</td>
			<%if(j>=imgPerRow){ j=0;%>
				</tr>
		<%	}
			
			j++;
		}%>	
		</table>
		<a href="movies.jsp?mode=search">&lt;&lt; Търсене</a>
	
	<%} %>
	
	
	
	
	<% 	
		// ### Add ###
		if (request.getParameter("mode")!=null && request.getParameter("mode").equals("add")) { 
					
	%>
		
		<h2 id="title">Добавяне на филм</h2>
		<form action="movies.jsp?mode=addmovie" method="post" id="edit" name="edit">
			<table class="formtable">
				<tr>
					<td align="right">Заглавие:</td>
					<td align="left"><input type="text" id="name" name="name"/></td>
				</tr>
				<tr>
					<td align="right">Описание:</td>
					<td align="left"><textarea cols="16" rows="5" id="description" name="description"></textarea></td>
				</tr>
				<tr>
					<td align="right">Времетраене (мин.):</td>
					<td align="left"><input type="text" id="lenght" name="lenght"/></td>
				</tr>
				<tr>
					<td align="right">Тип:</td>
					<td align="left">
						<select id="type" name="type">
							<%=UIUtils.printOptions("select CS01_NOMENCLATURE_ID, CS01_DESCRIPTION from TUDB.TS01_NOMENCLATURES where CS01_NOMENCLATURE_ID_FROM = 200", 0)%>
						</select>
					
					</td>
				</tr>
				<tr>
					<td></td>
					<td><input type="submit" id="submit" name="submit" value="Запази"/>
				</tr>
			</table>
		</form>
		
		<a href="movies.jsp?mode=search">&lt;&lt; Търсене</a>
	
	<%} %>

</div>

<%@include file="template/footer.jspf" %>